<cfcomponent name="User" hint="" extends="com.revenea.account.DAO">


	<!--- Public methods --->


	<cffunction name="load" returnType="query" access="public" output="false" hint="">
		<cfargument name="oObject" type="com.revenea.account.data.User" required="true" hint="The object we're asking to be loaded" />
		<cfset var local = StructNew() />
		<!--- There are two potential ways to load a User --->
		<cfif (arguments.oObject.getId() GT 0)>
			<!--- If the id is non-zero, try to load that way --->
			<cfquery name="local.qReturn" datasource="#getDataSource()#">
				SELECT user_id, username, person_id
				FROM users
				WHERE
					(is_active = 1)
					AND (user_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.oObject.getId()#" />)
			</cfquery>
		<cfelseif (Len(arguments.oObject.getUsername()) GT 0)>
			<!--- As a fallback we will try to laod via the username, which happens during an authentication --->
			<cfquery name="local.qReturn" datasource="#getDataSource()#">
				SELECT user_id, username, person_id
				FROM users
				WHERE
					(is_active = 1)
					AND (username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oObject.getUsername()#" />)
			</cfquery>
		</cfif>
		<cfreturn local.qReturn />
	</cffunction>

	<cffunction name="add" returnType="numeric" access="public" output="false" hint="">
		<cfargument name="oUser" type="com.revenea.account.data.User" required="true" hint="The authenticated user making the addition" />
		<cfargument name="oObject" type="com.revenea.account.data.User" required="true" hint="The object being added to the system" />
		<cfset var local = StructNew() />
		<cfquery name="local.qAdd" datasource="#getDataSource()#" result="local.stResult">
			INSERT INTO users (username, password, person_id, is_active)
			VALUES
				(
					<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oObject.getUsername()#" />
					,''
					,<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.oObject.getPerson().getId()#" />
					,1
				)
		</cfquery>
		<!--- NOTE: This next part only works for MySQL. --->
		<cfreturn local.stResult.generated_key />
	</cffunction>

	<cffunction name="edit" returnType="boolean" access="public" output="false" hint="">
		<cfargument name="oUser" type="com.revenea.account.data.User" required="true" hint="The authenticated user making the edit" />
		<cfargument name="oObject" type="com.revenea.root.Object" required="true" hint="The object we're trying to edit" />
		<cfset var local = StructNew() />
		<cfquery name="local.qEdit" datasource="#getDataSource()#">
			UPDATE users
			SET
				username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oObject.getUsername()#" />
				,person_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.oObject.getPerson().getId()#" />
			WHERE
				(is_active = 1)
				(user_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.oObject.getId()#" />)
		</cfquery>
		<cfreturn true />
	</cffunction>

	<cffunction name="remove" returnType="boolean" access="public" output="false" hint="">
		<cfargument name="oUser" type="com.revenea.account.data.User" required="true" hint="The authenticated user that is performing the remove operation" />
		<cfargument name="oObject" type="com.revenea.root.Object" required="true" hint="The object being removed from the system" />
		<cfset var local = StructNew() />
		<cfquery name="qRemove" datasource="#getDataSource()#">
			UPDATE users
			SET is_active = 0
			WHERE (user_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.oObject.getId()#" />)
		</cfquery>
		<cfreturn true />
	</cffunction>

	<cffunction name="authenticate" returntype="query" access="public" output="false" hint="Returns all records that match both the supplied User and password.">
		<cfargument name="oUser" type="com.revenea.account.data.User" required="true" hint="The user we are going to try to authenticate" />
		<cfargument name="sPassword" type="string" required="true" />
		<cfset var qReturn = "" />
		<cfquery name="qReturn" datasource="#getDatasource()#">
			SELECT user_id, username, person_id
			FROM users
			WHERE
				(is_active = 1)
				AND (username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oUser.getUsername()#" />)
				AND (password = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="20" value="#arguments.sPassword#" />)
		</cfquery>
		<cfreturn qReturn />
	</cffunction>

	<cffunction name="setPassword" returntype="boolean" access="public" output="false" hint="Sets the password to string provided for the specified user.">
		<cfargument name="oUser" type="com.revenea.account.data.User" required="true" hint="The User who is about to have their password set." />
		<cfargument name="sPassword" type="string" required="true" hint="The password to be set for the specified User." />
		<cfset var local = StructNew() />
		<cfquery name="local.qSetPassword" datasource="#getDatasrouce()#">
			UPDATE users
			SET password = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="20" value="#arguments.sPassword#" />
			WHERE (user_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.oUser.getId()#" />)
		</cfquery>
		<cfreturn true />
	</cffunction>

	<cffunction name="getAll" returnType="query" access="public" output="false" hint="Supports the collection, returns a record set containing all Users in the system.">
		<cfset var local = StructNew() />
		<cfquery name="local.qReturn" datasource="#getDataSource()#">
			SELECT user_id, username, person_id
			FROM users
			WHERE (is_active = 1)
			ORDER BY username
		</cfquery>
		<cfreturn local.qReturn />
	</cffunction>


</cfcomponent>